<?php 
require_once("../includes/config.php"); 
  $owners= query("SELECT * FROM apartments_owner WHERE user_id = ?",$_SESSION['id']);
  foreach($owners as $owner){
    $owner_data["first_name"] = $owner['first_name'];
    $owner_data["last_name"]= $owner['last_name'];
    $owner_data["username"]= $owner['username'];
    $owner_data["email"] = $owner['email'] ;
  }
  
  if($_SERVER["REQUEST_METHOD"] == "POST"){
    $username = $_POST['username'];
    $password = sha1($_POST['password']);
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email = $_POST['email'];
    $owners1= query("SELECT * FROM apartments_owner WHERE username= ? and user_id != ?",$username,$_SESSION['id']);
    if(!empty($owners1)){
      $_SESSION['error_message']="اسم المستخدم موجود مسبقاً";
    }
    else{
      $update= query("UPDATE apartments_owner SET first_name =? ,last_name=?,password=? ,username=? ,email = ? WHERE user_id =?",$first_name,$last_name,$password,$username,$email,$_SESSION['id']);
        $_SESSION['success_message']="تم الحفظ";
       redirect('view_profile.php');
    }
  }
  render("view_profile.php", array("title"=>"الملف الشخصى" ,"owner_data"=>$owner_data));

?>